$(function () {
	//新增
	$(document).on("click", "#add", function () {
		$("#md-modal").find(".md-content").find("h3").text("新增");
		xx_clear_modal();
		xx_event_add();
	}); 
	
	//编辑
	$(document).on("click", "#edit", function () {
		$("#info-modal").find(".modal-header").find("h3").text("编辑");
		var id = $(this).data("id");
		xx_clear_modal();
		//查询角色详情
		$.ajax({
			type: 'GET',
			data:{'id':id},
			dataType: 'json',
			url: "info.html",
			success: function(response) {
				if(response.operateMsg == 200){
					//数据回显
					xx_event_show(response);
				}else{
					xx_alert("error", "");
				}
			},
			error:function(){
				xx_alert("error", "");
			}
		});
	});
	
	//提交表单
	$(document).on("click", "#sub", function () {
		$.ajax({
			type: 'POST',
			dataType : 'json',
			data: $("#detailForm").serialize(),
			url: "save.html",
			success: function(response) {
				if(response.operateMsg == 200){
					xx_alert("success", "");
					$("#info-modal").modal("hide");
					xx_initData(1);
				}else{
					xx_alert("error", "");
				}
			},
			error:function(){
				xx_alert("error", "");
			}
		});
	});
	
	//删除
	$(document).on("click", "#delete", function () {
		var ids = $(this).data("id");
		
		swal({
			title: "确定删除?",
			text: "",
			type: "warning",
			showCancelButton: true,
			confirmButtonColor: "#DD6B55",
			confirmButtonText: "确认删除!",
			cancelButtonText: "取消",
			closeOnConfirm: false,
			closeOnCancel: false
		},
		function(isConfirm){
			if (isConfirm) {
				xx_event_del(ids);
			} else {
				xx_alert("error", "取消删除！");
			}
		});
	});
	
	//批量删除
	$(document).on("click", "#delBath", function () {
		var ids = "";
		//将已选的id拼在一起
		$("input:checkbox[name=ck]:checked").each(function (i) {
			if (0 == i) {
				ids = $(this).val();
			} else {
				ids += (","+$(this).val());
			}
		});
		//id为空
		if (ids == "") {
			xx_alert("info", "请选择要删除的数据！");
			return false;
		}else {
			
			swal({
				title: "确定删除?",
				text: "",
				type: "warning",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确认删除!",
				cancelButtonText: "取消",
				closeOnConfirm: false,
				closeOnCancel: false
			},
			function(isConfirm){
				if (isConfirm) {
					xx_event_del(ids);
				} else {
					xx_alert("error", "取消删除！");
				}
			});
		}
	});
	
	//表单清除
	function xx_clear_modal() {
		$("#detailForm input").val("");
	};
	
	/*//分页插件
	$(document).on("click", '#pagination>li', function(){
		$this = $(this);
		var pageIndex = $this.data('page');
		xx_event_page(pageIndex);
	});*/
	
	/*//刷新数据
	$(document).on("click", '.reload', function(){
		$.each($("#pagination>li"),function(){
			$this = $(this);
			if($this.hasClass("active")){
				var pageIndex = $this.data('page');
				xx_event_page(pageIndex);
			};
		});
	});*/
	
});	

//初始化数据
/*function xx_initData(pageIndex){
	$.ajax({
		type: 'GET',
		data:{'pageIndex':pageIndex,'pageSize':10},
		dataType: 'html',
		url: "query.html",
		success: function(response) {
			$("#xx_list_data").html(response);
		},
		error:function(){
			xx_alert("error", "");
		}
	});
};*/

//批量删除
function xx_event_del(ids){
	$.ajax({
		type: 'GET',
		dataType : 'json',
		data: {'ids':ids},
		url: "delete.html",
		success: function(response) {
			xx_alert("success", "共删除了"+response.delNum+"条数据！");
			xx_initData(1);
		}
	});
}
